package com.bml.architect.spring.meta;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * spring 内部提供的读取元数据的api
 * 可读取class元数据库和注解
 */
public class MetaTest {
    public static void main(String[] args) throws IOException {
        SimpleMetadataReaderFactory simpleMetadataReaderFactory = new SimpleMetadataReaderFactory();

        MetadataReader metadataReader = simpleMetadataReaderFactory.getMetadataReader("com.bml.architect.spring.service.UserService");
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        System.out.println("获取class名称："+classMetadata.getClassName());
        System.out.println("获取第一个接口名字："+classMetadata.getInterfaceNames()[0]);
        System.out.println("获取第一个内部类名字："+classMetadata.getMemberClassNames()[0]);
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        // 获取一个AnnotationMetadata，并获取类上的注解信息
        for (String annotationType : annotationMetadata.getAnnotationTypes()) {
            System.out.println("注解类型："+annotationType);
        }
        System.out.println("是否实现注解Component："+annotationMetadata.hasAnnotation(Component.class.getName()));
        //hasMetaAnnotation可以找到传递的依赖注解
        System.out.println("是否实现注解Component:"+annotationMetadata.hasMetaAnnotation(Component.class.getName()));

    }
}
